<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://unpkg.com/vue@next"></script>
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script>
      const NotFoundComponent = { template: "<p>Page not Found</p>" };
      const HomeComponent = { template: "<p>Home Page</p>" };
      const AboutComponent = { template: "<p>About Page</p>" };

      // 路由策略
      const routes = {
        "/": HomeComponent,
        "/about": AboutComponent,
      };

      // 路由组件
      const SimpleRouter = {
        data: () => ({
          currentRoute: window.location.pathname,
        }),

        computed: {
          CurrentComponent() {
            return routes[this.currentRoute] || NotFoundComponent;
          },
        },

        // 渲染
        render() {
          return Vue.h(this.CurrentComponent);
        },
      };

      const app = Vue.createApp(SimpleRouter).mount("#app");

      setTimeout(() => {
        app.currentRoute = "/about";
        history.pushState(null, "about", "/about");
      }, 1000);
    </script>
  </body>
</html>
